package com.tcm.ucenter.web.controller;

import com.base.core.utils.R;
import com.tcm.ucenter.domain.dto.PatientDTO;
import com.tcm.ucenter.service.PatientService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ucenter/patient")
public class PatientController {

    @DubboReference(version = "0.0.1-alpha", timeout = 3000, check = false, retries = 3)
    private PatientService patientService;

    /**
     * 根据id获取但条数据
     * @param id
     * @return
     * @throws Exception
     */
    @GetMapping("/view")
    public R view(@RequestParam("id") Long id) throws Exception {
        return R.success(patientService.findById(id));
    }

    /**
     * 根据id获取但条数据
     * @param userId
     * @return
     * @throws Exception
     */
    @GetMapping("/list")
    public R listPatient(@RequestParam("userId") Long userId) throws Exception {
        return R.success(patientService.listPatient(userId));
    }

    /**
     * 新增患者信息
     * @param patientDto
     * @return
     */
    @PostMapping("/save")
    public R create(@RequestBody PatientDTO patientDto){
        try{
            patientService.save(patientDto);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 更新数据
     * @param patientDto
     * @return
     */
    @PostMapping("/update")
    public R update(@RequestBody PatientDTO patientDto){
        try{
            patientService.update(patientDto);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

    /**
     * 逻辑删除
     * @param id
     * @return
     */
    @GetMapping("/delete")
    public R delete(@RequestParam("id") Long id){
        try{
            patientService.deleteLogic(id);
        }catch (Exception e){
            return R.fail(e.getMessage());
        }
        return R.success();
    }

}
